package com.shuimuzhen.SecondhandMotorManagementSystem.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;



    // 设置字符串值,默认60min失效
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value,3600, TimeUnit.SECONDS);
    }

    // 获取字符串值
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 设置字符串值并设置过期时间（单位：秒）
    public void setWithExpire(String key, String value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    // 判断键是否存在
    public boolean hasKey(String key) {
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }

    // 删除键
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    // 其他操作，如List、Set、Hash等操作可以类似地封装
    // ZSet 相关操作
    public void addToZSet(String key, Object value, double score) {
        redisTemplate.opsForZSet().add(key, value, score);
    }

    public Double getZSetScore(String key, Object value) {
        return redisTemplate.opsForZSet().score(key, value);
    }

    public Long getZSetRank(String key, Object value) {
        return redisTemplate.opsForZSet().rank(key, value);
    }

    public Long getZSetCount(String key, double min, double max) {
        return redisTemplate.opsForZSet().count(key, min, max);
    }

    public Long getZSetSize(String key) {
        return redisTemplate.opsForZSet().zCard(key);
    }

    /**
     *
     * @param stationId
     * @return 如果 stationId 已存在：Redis 会将其分数增加指定的数值（在这个例子中是 1）。
     * 如果 stationId 不存在：Redis 会将 stationId 添加到 "hotStations" 有序集合中，并且将其分数设置为 1。
     */

    public Double incrHotStation (int stationId){
        return   redisTemplate.opsForZSet().incrementScore("hotStations", stationId, 1);
    }

    public Set<Object> getHotStaions(int size){
        return redisTemplate.opsForZSet().reverseRange("hotStations", 0, size-1);
    }
    // Hash 相关操作
    public void putHash(String key, String hashKey, Object value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    public Object getHash(String key,String hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }

    public Boolean hasHashKey(String key,String hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }

    public Long getHashSize(String key) {
        return redisTemplate.opsForHash().size(key);
    }

    public void deleteHash(String key, String... hashKeys) {
        redisTemplate.opsForHash().delete(key, hashKeys);
    }
}
